import type {
  ImportStudentPayload,
  ImportStudentSuccessData,
  LoginPayload,
  LoginSuccessData,
  SearchUserPayload,
  SearchUserSuccessData,
  UpdatePasswordPayload,
  UpdatePasswordSuccessData,
  UserProfileSuccessData
} from './types'
import { fetcher } from '@/apis/fetcher'

export * from './composables'
export { userMessage, UserCustomCode } from './message'
export type { UserProfile } from './types'

export function ApiLogin(body: LoginPayload) {
  return fetcher<LoginSuccessData>('/users/login', {
    method: 'POST',
    body
  })
}

export const KeyApiGetUserInfo = ['users', 'profile'] as const
export function ApiGetUserInfo() {
  return fetcher<UserProfileSuccessData>('/users/profile')
}

export const KeyApiUpdatePassword = ['users', 'password'] as const
export function ApiUpdatePassword({ username, ...body }: UpdatePasswordPayload) {
  return fetcher<UpdatePasswordSuccessData>(`/users/${username}/password`, {
    method: 'PATCH',
    body
  })
}

export function ApiSearchUser(query?: SearchUserPayload) {
  return fetcher<SearchUserSuccessData>('/users', { query })
}

export const KeyApiImportStudent = ['users', 'import'] as const
export function ApiImportStudent(body: ImportStudentPayload) {
  return fetcher<ImportStudentSuccessData>('/users/import', {
    method: 'POST',
    body
  })
}

export function ApiRefreshToken(refreshToken: string) {
  return fetcher<LoginSuccessData>('/users/refresh', {
    method: 'POST',
    body: { refreshToken }
  })
}
